﻿using Microsoft.Win32;
using System.IO;
using System.Runtime;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Forms;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using MessageBox = System.Windows.MessageBox;
using UserControl = System.Windows.Controls.UserControl;

namespace MaterialDesignDemo.UserControls
{
    /// <summary>
    /// AuthorityManagerPage.xaml 的交互逻辑
    /// </summary>
    public partial class AuthorityManagerPage : UserControl
    {
        public AuthorityManagerPage()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                _TextBox.Text = dialog.SelectedPath;
                Load_TreeView(dialog.SelectedPath);
            }
        }
        public void Load_TreeView(string rootPath)
        { 
            TreeViewItem rootNode = new TreeViewItem();
            rootNode.Header = "根目录";
            _TreeView.Items.Clear();
            // 加载子文件夹和文件
            LoadSubDirectory(rootNode,rootPath);

            // 将根节点添加到TreeView中
            _TreeView.Items.Add(rootNode);
        }

        private void LoadSubDirectory(TreeViewItem node,string path)
        {
            try
            {
               DirectoryInfo directoryInfo = new DirectoryInfo(path);

                foreach (DirectoryInfo subDirectory in directoryInfo.GetDirectories())
                { 
                    TreeViewItem subNode = new TreeViewItem();
                    subNode.Header = subDirectory.Name;
                    LoadSubDirectory(subNode, subDirectory.FullName);
                    node.Items.Add(subNode);
                }
                //加载文件
                foreach (FileInfo fileInfo in directoryInfo.GetFiles())
                {
                    TreeViewItem subNode = new TreeViewItem();
                    subNode.Header = fileInfo.Name;

                    node.Items.Add(subNode);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void _TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            // 获取选中的节点
            TreeViewItem selectedNode = _TreeView.SelectedItem as TreeViewItem;

            // 显示选中节点的Header
            if (selectedNode != null)
            {
                MessageBox.Show(selectedNode.Header.ToString());
            }
        }

        
    }
}
